package com.yr.ryjk.festival.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yr.ryjk.common.Pagination;
import com.yr.ryjk.festival.dao.FestivalMapper;
import com.yr.ryjk.festival.entity.Festival;
import com.yr.ryjk.param.dao.ParamMapper;
import com.yr.ryjk.param.entity.Key;
import com.yr.ryjk.param.entity.Param;
import com.yr.ryjk.util.IDGenerator;

@Service
public class FestivalService implements IFestivalService{
	@Autowired
	private FestivalMapper festivalMapper;
	@Autowired
	private ParamMapper paramMapper;
	
	@Override
	public Festival getFesById(String id) {
		return festivalMapper.selectByPrimaryKey(id);
	}

	@Override
	public void add(Festival fes) {
		fes.setId(IDGenerator.gen32UUID());
		festivalMapper.insert(fes);
		
	}

	@Override
	public void delete(String ids) {
		festivalMapper.deleteByIds(ids);
	}

	@Override
	public void modify(String id, String picUrl,String festivalName, 
			Date startDate, Date endDate) {
		Festival festival = new Festival();
		festival.setId(id);
		festival.setFestivalName(festivalName);
		festival.setPicUrl(picUrl);
		festival.setStartDate(startDate);
		festival.setEndDate(endDate);
		festivalMapper.updateByPrimaryKeySelective(festival);
	}

	@Override
	public Map<String, Object> getListOfPage(int pageNo, int pageSize) {
		List<Festival> list = festivalMapper.selectOfPage((pageNo - 1) * pageSize,pageSize);
		int count = festivalMapper.selectCount();
		Pagination pager = new Pagination();
		pager.setPageNo(pageNo);
		pager.setPageSize(pageSize);
		pager.setTotalCount(count);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("pagination", pager);
		return map;
	}

	@Override
	public List<Festival> getFitList() {
		Param param=(paramMapper.selectByPrimaryKey(Key.festival_ads.getKey()));
		int count=Integer.parseInt(param.getValue());
		return festivalMapper.selectFitList(new Date(),count);
	}
}
